package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.utilities.AlreadyExistingElementException;
import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile dell'aggiunta testo
 */
public class TextAddingServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		String corso=(String) ses.getAttribute("corso");
		String choise=req.getParameter("scelta");
		
		String nome=req.getParameter("nome");
		if(nome==null || nome.equals("")){
			nome="___";
		}
		nome=nome.replace(" ", "_");
		nome=Setup.getSetup().getMediator().getGpService().changeInputText(nome);
		int id=0;
		String text=new String();
		if(req.getParameterMap().containsKey("cont")){
			id=Integer.parseInt(req.getParameter("cont"));
		} else {
			if(req.getParameterMap().containsKey("testo")){
				text=req.getParameter("testo");
				text=Setup.getSetup().getMediator().getGpService().changeHTMLText(text);
			} else {
				resp.sendRedirect("./html/errorpages/pagina404.html");
				return;
			}
		}
		List<String> attr=new ArrayList<String>();
		addToAttributes(attr, "color", req);
		addToAttributes(attr, "font-size", req);
		addToAttributes(attr, "font", req);
		addToAttributes(attr, "stile", req);
		addToAttributes(attr, "weight", req);
		addToAttributes(attr, "dec", req);
		addToAttributes(attr, "al", req);
		String current=(String) req.getSession().getAttribute("current");
		try {
			if(!req.getParameterMap().containsKey("cont")){
					id=Setup.getSetup().getMediator().geteService().insertElement(corso,choise, text);
			}
			Setup.getSetup().getMediator().geteService().appendContent(corso,current, nome, choise, id, attr);
			resp.sendRedirect("./view?page="+current);
		} catch (AlreadyExistingElementException e) {
			resp.sendRedirect("./html/errorpages/nomecontenuto.html"); 
		} catch (PageNotFoundException e) {
			resp.sendRedirect("./html/errorpages/pagina404.html");
		}
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else {
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	}
	
	public void addToAttributes(List<String> attributes, String idpar, HttpServletRequest req){
		String buf=req.getParameter(idpar);
		if(buf!=null){
			attributes.add(buf);
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession ses=req.getSession(false);
		
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		PrintWriter out=resp.getWriter();
		
		out.write("<html>\n");
		out.write("<head>\n");
		out.write("<title>Conferma</title>\n");
		out.write("<link rel='stylesheet' type='text/css' href='./html/LOGIN/login2.css'/>\n");
		out.write("</head>\n");
		out.write("<body>\n");
		out.write("<div id='contents'>\n");
		out.write("<p id='notifica'> Caricamento avvenuto con successo!</p>\n");
		out.write("<a href='./view?page="+req.getSession(false).getAttribute("current")+"'>");     
		out.write("<input type='button' value='Indietro' id='submit' name='submit'/></a>\n");
		out.write("</div>\n");
		out.write("<div id='footer'> Powered by JwTeam</div>\n");
		out.write("</body>\n");
		out.write("</html>\n");
		} else{
			resp.sendRedirect("./html/errorpages/nopermessi.html");
			return;
		}
	} else {
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
	}
		
}
	
	
	
	
}
